<template>
  <div>
    <a-page-header class="pt-0" @back="back" :title="t('填写表单')"></a-page-header>
    <a-spin :spinning="table.loading" class="w-[100%]">
      <a-form
        layout="vertical"
        @finish="submit"
        :rules="formRules"
        :model="form"
        ref="formRef"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 8 }"
      >
        <a-card :bordered="false" :body-style="{ paddingTop: '0' }">
          <a-form-item label="字典名称" name="dictName">
            <a-input allow-clear v-model:value="form.dictName" />
          </a-form-item>

          <a-form-item label="字典值" name="dictValue">
            <a-input allow-clear v-model:value="form.dictValue" />
          </a-form-item>
          <a-form-item label="字典排序" name="dictSort">
            <a-input-number :min="0" class="w-[100%]" allow-clear v-model:value="form.dictSort" />
          </a-form-item>

          <a-form-item label="字典类型" name="dictType">
            <a-input allow-clear disabled v-model:value="form.dictType" />
          </a-form-item>

          <div class="flex gap-4">
            <a-button type="primary" htmlType="submit">{{ t('保存') }}</a-button>
            <a-button @click="reset">{{ t('重置') }}</a-button>
          </div>
        </a-card>
      </a-form>
    </a-spin>
  </div>
</template>

<script setup lang="ts">
import type { IResponse } from '@/api/config/types';
import { list } from '../data/curd';
import { allowEditMenu, table } from '../data/data';
import { formRef, form, formRules } from '../data/form';
const { t } = useI18n();
import { addDictData, updateDictData } from '@/api/modules/auth/dict.data';

const back = () => {
  allowEditMenu.value = false;
  reset();
};
const submit = async () => {
  table.value.loading = true;
  let result: IResponse | undefined = undefined;
  if (!form.value.dictId) {
    const { data } = await addDictData(form.value);
    result = data;
    /* 添加 */
  } else {
    const { data } = await updateDictData(form.value);
    result = data;
    /* 修改 */
  }
  await list();
  back();
};
const reset = () => {
  formRef.value?.resetFields();
};
</script>

<style lang="scss" scoped></style>
